#ifndef __INVADER_ARMY_H__
#define __INVADER_ARMY_H__

#include <map>

#include <QFrame>
#include <QTimer>

#include "invader.h"

/**
 * The collection of Invaders which are trying to destroy the Tank and invade
 * planet Earth
 */
class InvaderArmy : public QFrame
{
  Q_OBJECT

public:
  /**
   * @param parent parent of this InvaderArmy
   */
  InvaderArmy (QWidget *parent = 0);

  /* rect is in InvaderArmy's local coordinates */
  /**
   * Attempts to hit an Invader in the InvaderArmy.  If an Invader is hit it
   * will no longer be visible.
   *
   * @param rect rectangle of the object trying to hit an Invader.  The
   *             position of this must be in the local InvaderArmy coordinates.
   */
  void tryHitInvader (const QRect &rect);
  /**
   * @returns the minimum X coordinate of all alive Invaders in the
   *          InvaderArmy's local coordinates.  The maximum Y coordinate of all
   *          Invaders in the same column is returned in maxY if provided.
   */
  int getMinX (int *maxY = 0) const;
  /**
   * @returns the maximum X coordinate of all alive Invaders in the
   *          InvaderArmy's local coordinates.  The maximum Y coordinate of all
   *          Invaders in the same column is returned in maxY if provided.
   */
  int getMaxX (int *maxY = 0) const;
  /**
   * @returns the minimum Y coordinate of all alive Invaders
   */
  int getMinY () const;
  /**
   * @returns the maximum Y coordinate of all alive Invaders
   */
  int getMaxY () const;
  /**
   * @returns the number of Invaders still alive
   */
  int getInvadersAlive () const { return _invadersAlive; }
  /**
   * @returns the number of milliseconds between InvaderArmy animation updates
   */
  int getAnimationInterval () { return _animationTimer->interval (); }

  /**
   * Override of the Qt sizeHint
   */
  virtual QSize sizeHint () const;

  /**
   * Total number of invaders in an InvaderArmy
   */
  static int numInvaders;

public slots:
  /**
   * Starts the InvaderArmy animation
   */
  void start ();
  /**
   * Stops the InvaderArmy animation
   */
  void stop ();
  /**
   * Resets the state of the InvaderArmy
   */
  void reset ();
  /**
   * Pauses or unpauses the InvaderArmy animation
   */
  void pause ();

signals:
  /**
   * Emitted when the Invaders in the InvaderArmy change animation frames
   */
  void frameChanged ();
  /**
   * Emitted when an Invader is killed
   *
   * @param score number of points you get for the killed Invader
   */
  void invaderKilled (int score);
  /**
   * Emitted when an Invader shoots at the Tank
   *
   * @param location location the bullet originated from
   */
  void invaderShoots (QPoint location);

protected:
  void resizeEvent (QResizeEvent *event);
  void moveEvent (QMoveEvent *event);

private:
  QTimer *_animationTimer;
  std::multimap<int, Invader *> _invaders;
  int _invadersAlive;
  int _newTimerInterval;

  void updateAnimationInterval ();

private slots:
  void animationTimerTimeout ();
  void invaderShot ();
};

#endif
